import 'package:flutter/material.dart';
import 'package:ninetyfour/l10n/app_localizations.dart';
import '../view/security_list_item.dart';
import '../controller/change_password.dart';
import '../controller/bing_page.dart';
import '../../manger/user_manger.dart';
import '../../components/toast_utils.dart';
import '../../components/custom_dialog.dart';
import '../controller/adress_page.dart';
import '../controller/pay_list_page.dart';

class SecurityPage extends StatelessWidget {
  const SecurityPage({super.key});

  List<Map<String, String>> securityOptions(BuildContext context) {
    final localizations = AppLocalizations.of(context);
    if (localizations == null) return [];  // 如果localizations为null，返回空列表
    
    return [
      {"title": localizations.changePassword, "desc": localizations.changeYourPasswordRegularlyToProtectYourAccount, "name": localizations.changePassword},
      {"title": localizations.emailVerification, "desc": localizations.usedForEmailAuthenticationAndSecurity, "name": localizations.verifyNow},
      {"title": localizations.phoneBinding, "desc": localizations.bindYourPhoneToLogInQuicklyWithYourNumber, "name": localizations.bindNow},
      {"title": localizations.zhangdandizhi, "desc": localizations.guanlidizhi, "name": localizations.bianji},
      {"title": localizations.jisuzhifu, "desc": localizations.kuairenyibu, "name": localizations.quchakan},
    ];
  }

  @override
  Widget build(BuildContext context) {
    final localizations = AppLocalizations.of(context);
    if (localizations == null) return const SizedBox.shrink();  // localizations为null时返回空视图
    
    final securityOptionsList = securityOptions(context);  // 获取安全选项列表

    // 这里是防止在传递给 ListView 时遇到null或不完整的选项
    return Scaffold(
      appBar: AppBar(
        toolbarHeight: 40,
        title: Text(localizations.accountSecurity,style: const TextStyle(fontSize: 17)),
      ),
      body: ListView.builder(
        itemCount: securityOptionsList.length,
        itemBuilder: (context, index) {
          final option = securityOptionsList[index];
          // 确保option中的值都不为null
          if (option['title'] == null || option['desc'] == null || option['name'] == null) {
            return const SizedBox.shrink();  // 返回空视图
          }
          return GestureDetector(
          onTap: () {
            final user = UserInfos.shared.userModel;
            if (index == 0) {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => ChangePasswordPage()),
              );
            } else if (index == 1) {
              if (user != null && user.email != null && user.email!.email.isNotEmpty){
                ToastUtils.showCenterToast(context, localizations.emailAlreadyBind);
                return;  
        
              } else {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => BingPage(titleName: localizations.emailVerification)),
                );  
              }
            } else if (index == 2) {
              if (user != null && user.mobile != null && user.mobile!.mobile.isNotEmpty){
               if (user.mobile != null && user.mobile!.status != 3) {
                  showLogoutDialog(context,localizations.changeAlreadyBind,user.mobile!.mobile);
                } else {
                  ToastUtils.showCenterToast(context, localizations.phoneAlreadyBind);
                  return;   
                }
              } else {
                  Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => BingPage(titleName: localizations.phoneBinding)),
                );
      
              }
            } else if (index == 3) {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => AddressPage()),
              );
            } else if (index == 4) {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => PaymentListPage()),
              );
            }
          },
          child: SecurityListItem(
            title: option['title']!,
            description: option['desc']!,
            actionText: option['name']!,
            indexs: index,
          ),
        );
        },
      ),
    );
  }

  void showLogoutDialog(BuildContext context,String title,String phone) {
    showDialog(
      context: context,
      builder: (context) => CustomDialog(
        title: title,
        description: "当前绑定手机号为$phone",
        leftButtonText: AppLocalizations.of(context)!.cancel,
        rightButtonText: AppLocalizations.of(context)!.confirm,
        onLeftPressed: () => Navigator.pop(context),
        onRightPressed: () {
          Navigator.pop(context);
          Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => BingPage(titleName: AppLocalizations.of(context)!.changePhone)),
          );
        },
      ),
    );
  }
}
